package com.starchen.optimalselection.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starchen.optimalselection.product.mapper.BrandMapper;
import com.starchen.optimalselection.product.mapper.CategoryBrandRelationMapper;
import com.starchen.optimalselection.product.model.CategoryBrandRelation;
import com.starchen.optimalselection.product.model.vo.CategoryBrandRelationVo;
import com.starchen.optimalselection.product.service.CategoryBrandRelationService;
import com.starchen.optimalselection.common.mapper.CategoryMapper;
import com.starchen.optimalselection.common.model.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelation> implements CategoryBrandRelationService {

    @Autowired
    private CategoryBrandRelationMapper relationMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<CategoryBrandRelationVo> getRelationByBrand(Long brandId){
        List<CategoryBrandRelationVo> categoryBrandRelationVoList = relationMapper.getCategoryBrandRelation(brandId);
        return categoryBrandRelationVoList;
    }

    @Override
    public List<Brand> getRelationByCategory(Long cateId) {
        List<CategoryBrandRelation> relations = relationMapper.selectList(new QueryWrapper<CategoryBrandRelation>().eq("category_id", cateId));

        List<Brand> brandList = new ArrayList<>();
        if(relations != null && relations.size() > 0){
            List<Long> brandIds = relations.stream().map(item ->
                    item.getBrandId()
            ).collect(Collectors.toList());
            brandList = brandMapper.selectBatchIds(brandIds);
        }
        return brandList;
    }

}